<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="objaggregation.examples.association.html">Object Aggregation examples</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.objaggregation.html">Object Aggregation 函数</a></div>
 <div class="up"><a href="objaggregation.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="objaggregation.examples2" class="section">
    <h2 class="title">范例</h2>
    <p class="para">
     We define 3 classes, each implementing a different storage method:
    </p>
    <p class="para">
     <div class="example" id="example-5025">
      <p><strong>Example #1 storage_classes.inc</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">FileStorage&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">FileStorage</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">write</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">name</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">WDDXStorage&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$version&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"1.0"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$_id</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"private"&nbsp;variable<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">WDDXStorage</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_genID</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">store</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_id</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$pid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">wddx_packet_start</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_id</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">wddx_add_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$pid</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"this-&gt;data"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$packet&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">wddx_packet_end</span><span style="color: #007700">(</span><span style="color: #0000BB">$pid</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$packet&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">wddx_serialize_value</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dba_open</span><span style="color: #007700">(</span><span style="color: #DD0000">"varstore"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"gdbm"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">dba_insert</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">uniqid</span><span style="color: #007700">(</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)),&nbsp;</span><span style="color: #0000BB">$packet</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">dba_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;a&nbsp;private&nbsp;method<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">_genID</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">uniqid</span><span style="color: #007700">(</span><span style="color: #0000BB">rand</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">DBStorage&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$dbtype&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"mysql"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">DBStorage</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">save</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"storage"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$serdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"insert&nbsp;into&nbsp;vars&nbsp;('</span><span style="color: #0000BB">$serdata</span><span style="color: #DD0000">',now())"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
    We then instantiate a couple of objects from the defined classes, and
    perform some aggregations and deaggregations, printing some object information
    along the way:
    </p>
    <p class="para">
     <div class="example" id="example-5026">
      <p><strong>Example #2 test_aggregation.php</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">"storageclasses.inc"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;some&nbsp;utilty&nbsp;functions<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">p_arr</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$k&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$v</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"\t</span><span style="color: #0000BB">$k</span><span style="color: #DD0000">&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$v</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">object_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"Class:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">get_object_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"property:&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #DD0000">&nbsp;(array)\n"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">p_arr</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"property:&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #DD0000">&nbsp;=&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">get_class_methods</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$method</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"method:&nbsp;</span><span style="color: #0000BB">$method</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br />}<br /><br /><br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">M_PI</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"kludge&nbsp;!=&nbsp;cruft"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;we&nbsp;create&nbsp;some&nbsp;basic&nbsp;objects<br /></span><span style="color: #0000BB">$fs&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">FileStorage</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$ws&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WDDXStorage</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;print&nbsp;information&nbsp;on&nbsp;the&nbsp;objects<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"\$fs&nbsp;object\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">object_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$fs</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"\n\$ws&nbsp;object\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">object_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$ws</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;do&nbsp;some&nbsp;aggregation<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"\nLet's&nbsp;aggregate&nbsp;\$fs&nbsp;to&nbsp;the&nbsp;WDDXStorage&nbsp;class\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">aggregate</span><span style="color: #007700">(</span><span style="color: #0000BB">$fs</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"WDDXStorage"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"\$fs&nbsp;object\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">object_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$fs</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nNow&nbsp;let&nbsp;us&nbsp;aggregate&nbsp;it&nbsp;to&nbsp;the&nbsp;DBStorage&nbsp;class\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">aggregate</span><span style="color: #007700">(</span><span style="color: #0000BB">$fs</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"DBStorage"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"\$fs&nbsp;object\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">object_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$fs</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nAnd&nbsp;finally&nbsp;deaggregate&nbsp;WDDXStorage\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">deaggregate</span><span style="color: #007700">(</span><span style="color: #0000BB">$fs</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"WDDXStorage"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"\$fs&nbsp;object\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">object_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$fs</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     We will now consider the output to understand some of the side-effects
     and limitation of object aggregation in PHP.
     First, the newly created <var class="varname"><var class="varname">$fs</var></var> and <var class="varname"><var class="varname">$ws</var></var>
     objects give the expected output (according to their respective class
     declaration). Note that for the purposes of object aggregation,
     <em class="emphasis">private elements of a class/object begin with an underscore 
     character (&quot;_&quot;)</em>, even though there is not real distinction between
     public and private class/object elements in PHP.
    </p>
    <p class="para">
     <div class="informalexample">
      <div class="example-contents">
<div class="cdata"><pre>
$fs object
Class: filestorage
property: data (array)
   0 =&gt; 3.1415926535898
   1 =&gt; kludge != cruft
method: filestorage
method: write

$ws object
Class: wddxstorage
property: data (array)
   0 =&gt; 3.1415926535898
   1 =&gt; kludge != cruft
property: version = 1.0
property: _id = ID::9bb2b640764d4370eb04808af8b076a5
method: wddxstorage
method: store
method: _genid
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     We then aggregate <var class="varname"><var class="varname">$fs</var></var> with the
     <strong class="classname">WDDXStorage</strong> class, and print out the object
     information. We can see now that even though nominally the
     <var class="varname"><var class="varname">$fs</var></var> object is still of
     <strong class="classname">FileStorage</strong>, it now has the property
     <var class="varname"><var class="varname">$version</var></var>, and the method <span class="function"><strong>store()</strong></span>,
     both defined in <strong class="classname">WDDXStorage</strong>. One important thing
     to note is that it has not aggregated the private elements defined in
     the class, which are present in the <var class="varname"><var class="varname">$ws</var></var> object. Also
     absent is the constructor from <strong class="classname">WDDXStorage</strong>, which
     will not be logical to aggegate.
    </p>
    <p class="para">
     <div class="informalexample">
      <div class="example-contents">
<div class="cdata"><pre>
Let&#039;s aggregate $fs to the WDDXStorage class
$fs object
Class: filestorage
property: data (array)
   0 =&gt; 3.1415926535898
   1 =&gt; kludge != cruft
property: version = 1.0
method: filestorage
method: write
method: store
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     The process of aggregation is cumulative, so when we aggregate
     <var class="varname"><var class="varname">$fs</var></var> with the class <strong class="classname">DBStorage</strong>,
     generating an object that can use the storage methods of all the
     defined classes.
    </p>
    <p class="para">
     <div class="informalexample">
      <div class="example-contents">
<div class="cdata"><pre>
Now let us aggregate it to the DBStorage class
$fs object
Class: filestorage
property: data (array)
   0 =&gt; 3.1415926535898
   1 =&gt; kludge != cruft
property: version = 1.0
property: dbtype = mysql
method: filestorage
method: write
method: store
method: save
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     Finally, the same way we aggregated properties and methods dynamically,
     we can also deaggregate them from the object. So, if we deaggregate the
     class <strong class="classname">WDDXStorage</strong> from <var class="varname"><var class="varname">$fs</var></var>, we
     will obtain:
    </p>
    <p class="para">
     <div class="informalexample">
      <div class="example-contents">
<div class="cdata"><pre>
And deaggregate the WDDXStorage methods and properties
$fs object
Class: filestorage
property: data (array)
   0 =&gt; 3.1415926535898
   1 =&gt; kludge != cruft
property: dbtype = mysql
method: filestorage
method: write
method: save
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     One point that we have not mentioned above, is that the process of
     aggregation will not override existing properties or methods in the
     objects. For example, the class <strong class="classname">FileStorage</strong> defines a
     <var class="varname"><var class="varname">$data</var></var> property, and the class
     <strong class="classname">WDDXStorage</strong> also defines a similar property 
     which will not override the one in the object acquired during
     instantiation from the class <strong class="classname">FileStorage</strong>.
    </p>
   </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="objaggregation.examples.association.html">Object Aggregation examples</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.objaggregation.html">Object Aggregation 函数</a></div>
 <div class="up"><a href="objaggregation.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
